<style lang="less" scope>
  @import "../assets/css/config.less";
  .menu-out{
    padding: 8px 10px;
    width: 100%;
    min-width: 200px;
    background-color: #F7F6F4;
    height: 100%;
    text-align: left;
    line-height: 30px;
    border-radius: 8px;
    cursor: default;
    overflow-y: auto;
    -ms-overflow-style:none; //兼容Edge
    scrollbar-color: transparent transparent; //兼容火狐
    scrollbar-arrow-color: @white;  /* 设置滚动条上的箭头颜色 兼容ie  */
    scrollbar-base-color: @white;  /* 设置滚动条的底色 */
    scrollbar-track-color: @white;  /* 设置滚动条块的背景色 */
    scrollbar-shadow-color: @white;  /* 设置箭头和滚动条右侧和底边的颜色 */
    .title{
      padding: 1px 10px;
      background-color: @white;
      border-radius: 4px;
      margin: 4px 0;
      font-size: 14px;
    }
    .subtitle{
      border: 2px solid @white;
      border-bottom: 0;
      padding: 0 15px;
      border-radius: 2px;
      font-size: 14px;
      &:nth-last-child(1){
        border-bottom: 2px solid @white;
      }
      &:hover{
        color: @color1;
        background-color: #F6E8E0;
      }
    }
    .active{
      color: @color1;
    }
  }

  // 兼容 chrome
  .menu-out::-webkit-scrollbar {
      width: 1px;
      background-color: #eee;
  }
  
  .menu-out::-webkit-scrollbar-track {
      background-color: #eee;
  }
  
  .menu-out::-webkit-scrollbar-thumb {
      background: @color2;
  }
  
  .menu-out::-webkit-scrollbar-button:start {
      // background: url(./imgs/up.png) no-repeat;
      background-size: 12px 12px;
  }
  
  .menu-out::-webkit-scrollbar-button:end {
      // background: url(./imgs/down.png) no-repeat;
      background-size: 12px 12px;
  }    
</style>

<template>
  <div id="my-own-menu">
    <div class="menu-out">
      <div v-for="(items, index) in menu" :key="items.title">
        <h3 class="title">{{items.title}}</h3>
        <div v-for="(item, key) in items.subtitle" class="subtitle" :class="active[0]===index && active[1]===key ? 'active' : ''" :key="item" @click="handleClick(index, key)">
          <p>{{item}}</p>
        </div>
      </div>
    </div>
    
  </div>
</template>
<script>
export default {
  name: "mymenu",
  data(){
    return{
    }
  },
  props:['menu'],
  mounted(){
    if (window.history && window.history.pushState) {
      // history.pushState(null, null, document.URL); //这里有没有都无所谓，最好是有以防万一
      window.addEventListener('popstate', this.goBack, false);
      // 回退时执行goback方法
    }
  },
  destroyed(){
    window.removeEventListener('popstate', this.goBack, false);
  },
  computed:{
    active:function(){
      let menu = this.$route.params.menu.split(',');
      return [parseInt(menu[1]), parseInt(menu[2])]
    }
  },
  methods: {
    goBack(){
      let ss =/^(\w+_)?\w+,\d+,\d+$/
      let url = this.$route.params.menu
      if(ss.test(url)){
        let list = url.split(',')
        this.$emit("handleMenuClick", {index: parseInt(list[1]), key: parseInt(list[2]), goback: true})
      }
    },
    handleClick(index, key){
      this.$emit("handleMenuClick", {index, key})
    }
  }
}
</script>